type PropsType = {
	value?: string | number
	colorful?: boolean
	currency?: "" | "$" | "￥"
	precision?: number
	notNumber?: 0 | "-"
	plus?: boolean
}
export const useNumber = (prop: PropsType) => {
	const { colorful, currency, precision, notNumber, plus } = prop
	function thousands(num: number | string) {
		const str = num.toString()
		const reg = str.indexOf(".") > -1 ? /(\d)(?=(\d{3})+\.)/g : /(\d)(?=(?:\d{3})+$)/g
		return str.replace(reg, "$1,")
	}

	function formatPrecision(num: number) {
		const times = Math.pow(10, precision ?? 0)
		return Math.round(num * times) / times
	}

	function getValue(v: string | number | undefined) {
		let num
		const vArray = (v + "").split("")
		const isPercent = vArray.pop() === "%"
		if (isPercent) num = Number(vArray.join(""))
		else num = Number(v)
		if (isNaN(num) && notNumber === "-") return notNumber
		else if (isNaN(num)) num = 0
		return (
			(num < 0 ? "-" : num > 0 && plus ? "+" : "") +
			(isPercent ? "" : currency ?? "$") +
			thousands(formatPrecision(Math.abs(num))) +
			(isPercent ? "%" : "")
		)
	}

	const formatedValue = getValue(prop.value ?? 0)

	function getStyle(v: number | string | undefined, type: number) {
		let num
		const vArray = (v + "").split("")
		const isPercent = vArray.pop() === "%"
		if (isPercent) num = Number(vArray.join(""))
		else num = Number(v) ?? 0
		return {
			color: type === 1 ? "#3b9b9e" : colorful ? (num > 0 ? "#4B7902" : num < 0 ? "#D9001B" : "") : "",
			cursor: type === 1 ? "pointer" : "default",
			overflow: "visible"
		}
	}

	return {
		formatedValue,
		getStyle,
		getValue,
		thousands,
		formatPrecision
	}
}
